/*
 * Copyright (c) 2009.
 */

package kz.home.gui;

import kz.home.db.DBUtils;
import kz.home.utils.Utils;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import java.util.Hashtable;
import java.util.Vector;

/**
 * check 
 * Created by IntelliJ IDEA.
 * User: danik
 * Date: 29.07.2009
 * Time: 21:51:56
 */
public class ChooseForm extends BaseForm{
    protected Vector data;
    protected Vector movies;
    protected Vector cinemas;
    protected Vector times;
    protected Command cmd_select;

    
    public ChooseForm(BaseForm mainScreen, Display display, Hashtable table, String cityId, Vector event) {
        super("Выберите кинотеатр или фильм", mainScreen, display, table);

        data = DBUtils.findInTable(DBUtils.CITIES, cityId);
        if (data != null && data.size() >= 5) {
            movies = getMovies("" + data.elementAt(4), event);
            cinemas = getCinemas("" + data.elementAt(3), event);

            if (cinemas != null) append("кинотеатры (" + cinemas.size() + ")");
            if (movies != null) append("Фильмы (" + movies.size() + ")");

            initCmdSelect();
        }
    }

    private void initCmdSelect() {
        cmd_select = new Command("Выбрать", Command.BACK, 2);
        addCommand(cmd_select);
    }

    private Vector getCinemas(String cinemasArr, Vector event) {
        return Utils.union(getCount(cinemasArr), getCount("" + event.elementAt(5)));
    }

    private Vector getMovies(String moviesArr, Vector event) {
        Vector movies = getCount(moviesArr);
        Vector eventMovie = getCount("" + event.elementAt(6));
        return  Utils.union(movies, eventMovie);
    }

    protected Vector getCount(String arr) {        
        return Utils.getTokens(arr.substring(1, arr.length() - 1), ",");
    }

    public void commandAction(Command command, Displayable displayable) {
        super.commandAction(command, displayable);

        if (command == cmd_select) {
            switch (getSelectedIndex()) {
                case 0: {
                    CinemaForm form = new CinemaForm(this, display,request, cinemas);
                    form.addOnScreen();
                    display.setCurrent(form);
                    break;
                }
                case 1: {
                    MoviesForm form = new MoviesForm(this, display, request, movies);
                    form.addOnScreen();
                    display.setCurrent(form);
                    break;
                }
            }
        }
    }
}
